// pages/doctor/doctor.js
const urlApi = require('../../utils/server.api.js')
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    GHId: [],
    GHIndex: -1,
    totalObj:[],
    totalPrice:0,
    notPayData:[],
    sickName:"",
    sickCard:"",
    allCheck:false,
    show:"show"
  },

  bindGHChange: function (e) {
    this.setData ({
      GHIndex: e.detail.value
    })
  },

  //获取待付款列表
  getNotPayList: function(){
    const that = this;
    wx.request({
      url: urlApi.getRegisterLogUrl(),
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        patientId: app.globalData.userInfo.patientId
      },
      success: function(res) {
        var GHId = []
        for (let i = 0; i < res.data.data.length; i++) {
          GHId[i] = res.data.data[i].regId;
        }
        that.setData({
          GHId: GHId
        })
      }
    })
  },

  create_chaxun:function(){
    const that = this;
    that.setData ({
      notPayData: []
    })
    wx.request({
      url: urlApi.getNotPayListUrl2() + "/" + that.data.GHId[that.data.GHIndex],
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (reponse) {
        console.log(reponse.data)
        if (reponse.data.code == 200 && reponse.data.data != null){
          var obj = reponse.data.data.careHistory;
          var arr1 = reponse.data.data.careOrders;
          var notPayList = new Array(); 
          for (let index = 0; index < arr1.length; index++) {        
            var arr = reponse.data.data.careOrders[index].careOrderItems;
            for (var i of arr) {
              if (i.status == 0){
                i.name = obj.patientName;
                i.price = i.amount;
                i.checked = false;
                i.id = i.itemId;
                notPayList.push(i);
              }
            }
          }
          if (notPayList.length != 0){
            that.setData({
              show: "hidden"
            })
          }
          that.setData({
            notPayData: notPayList
          })
        }
        if (reponse.data.code == 400 && reponse.data.data == null) {
          that.setData({
            show: "show"
          })
        }
      }
    })
  },

  //选择事件
  handleClick:function(e){
    var itemId = e.currentTarget.dataset.id;
    var price  = e.currentTarget.dataset.price;
    if (this.data.totalObj.indexOf(itemId) > -1) {//则包含该元素
      var arr = new Array();
      this.data.totalObj.map(function(i){
        if (i !== itemId){
          arr.push(i)
        }
      })
      this.data.totalObj = arr;
      this.countPrice(-price)
    }else{
      this.data.totalObj.push(itemId)
      this.countPrice(price)
    }
    
  },

  //计算总价
  countPrice:function(price){
    console.log("计算总价",price)
    this.setData({
      totalPrice: this.data.totalPrice + price,
    })
    this.setData({
      toPayColor: this.data.totalPrice == 0 ? "toPayColor2" :"toPayColor",
    })
  }, 

  allChoose:function(e){
    this.setData({
      allCheck: !this.data.allCheck
    })
    if (this.data.allCheck){
      console.log("选中")
      var total = 0;
      for (var i of this.data.notPayData) {
        total = total + i.price;
      }
      var arr = this.data.notPayData;
      var totalObj = new Array();
      for (var i of arr) {
        i.checked = true;
        totalObj.push(i.id);
      }
      this.setData({
        totalPrice: total,
        notPayData: arr,
        totalObj: totalObj,
        toPayColor:"toPayColor"
      })
    }else{
      var arr = this.data.notPayData;
      for (var i of arr) {
        i.checked = false;
      }
      this.setData({
        totalPrice: 0,
        notPayData: arr,
        totalObj:[],
        toPayColor: "toPayColor2"
      })
    }

    
    
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      sickName: app.globalData.userInfo.name,
      sickCard: app.globalData.userInfo.idCard
    })
    this.getNotPayList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  toPay: function (e) {
    wx.showModal({
      title: '提示',
      content: '暂不支持小程序支付，请到医院前台支付',
      complete: (res) => {
        if (res.cancel) {
          
        }
    
        if (res.confirm) {
          
        }
      }
    })
    // var info = {
    //   money: this.data.totalPrice
    // }
    // app.globalData.JE = this.data.totalPrice
    // wx.navigateTo({
    //   url: '/pages/MZPay/MZPay?info=' + JSON.stringify(info)
    // })
  }
})
